import tkinter
from tkinter import messagebox


def main():
    flag = True

    def changeText():
        nonlocal flag
        flag = not flag
        color, msg = ('red', 'hello world!') \
            if flag else ('blue', "oh,good")
        label.config(text=msg, fg=color)

    def exit_GUI():
        if tkinter.messagebox.askokcancel('exit now?', 'see you latter'):
            top.quit()

    # 创建上层窗口
    top = tkinter.Tk()
    # 十二指窗口大小
    top.geometry('240x160')
    # 设置窗口标题
    top.title('tkinter GUI')

    # 创建事件对象并绑定添加至窗口
    label = tkinter.Label(top, text='hello,world!', font='Arial -32', fg='red')
    # 添加对象并居中
    label.pack(expand=1)
    # c创建状=装按钮的容器
    container = tkinter.Frame(top)
    # 使用容器装在按钮,传入容器，label标签，已经按钮触发的方法
    button01 = tkinter.Button(container, text='修改', command=changeText)
    button01.pack(side='left')
    button02 = tkinter.Button(container, text='退出', command=exit_GUI)
    button02.pack(side='left')
    container.pack(side='bottom')
    # 开启主事件循环
    tkinter.mainloop()


if __name__ == '__main__':
    main()
